package suncertify.db.test;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class test3 {

    public static Lock lock = new ReentrantLock();
    public static Condition lockCondition = lock.newCondition();

    public static void main(String[] args) throws InterruptedException {

	// lock.lock();

	// CountDownLatch startSignal = new CountDownLatch(1);
	// CountDownLatch doneSignal = new CountDownLatch(1);
	//
	// Thread workerThread = new Thread(new Worker(startSignal, doneSignal));
	//
	// // System.out.println("MainThread before workerThread.start();");
	// workerThread.start();
	// Data.log("", "MainThread after workerThread.start();");
	//
	// // Thread.sleep(1000);
	// Data.log("", "MainThread before startSignal.countDown();");
	// startSignal.countDown(); // let all threads proceed
	// Data.log("", "MainThread after startSignal.countDown();");
	//
	// Data.log("", "MainThread before doneSignal.await();");
	// long t1 = System.nanoTime();
	// doneSignal.await(3, TimeUnit.SECONDS); // wait for all to finish
	// long t2 = System.nanoTime() - t1;
	// Data.log("", "MainThread after wait. Waited for: " + t2 / 1000000);
	//
	// //
	// Data.log("", "MainThread before workerThread.interrupt();");
	// workerThread.interrupt();
	// Data.log("", "MainThread after workerThread.interrupt();");
	//
	// // Thread.sleep(10000);
	// Data.log("", "Main thread finished!");
    }
}

class Worker implements Runnable {
    private final CountDownLatch startSignal;
    private final CountDownLatch doneSignal;

    Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
	this.startSignal = startSignal;
	this.doneSignal = doneSignal;
    }

    public void run() {
	try {
	    // Thread.sleep(1000);
	    // Data.log("", "WorkerThread before startSignal.await();");
	    startSignal.await();
	    // Data.log("", "WorkerThread AFTER startSignal.await();");

	    // Data.log("", "WorkerThread before test3.lock.lock();");
	    try {
		test3.lock.lock();
		test3.lockCondition.await();
	    } finally {
		test3.lock.unlock();
	    }
	    // Thread.sleep(234);
	    // Data.log("", "WorkerThread Finished! &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");

	} catch (InterruptedException ex) {
	    // Data.log("", "WorkerThread: InterruptedException !!!!!!!!!!!!!!!!!!!!!!!!!");
	} catch (Exception ex) {
	    // Data.log("", "WorkerThread: Exception");
	} finally {
	    doneSignal.countDown();
	}
    }

    private void doWork() throws IOException {

	File file = new File("C:\\Infor\\Expert_WMWares\\DEDAVWPerf2k8b.zip");
	RandomAccessFile raf = new RandomAccessFile(file, "rw");

	// Thread.currentThread().sleep(7000);
	for (int i = 0; i < 2000000; ++i) {
	    byte b = raf.readByte();
	}
	raf.close();
    }
}
